(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.intindex.createController', createController);
	createController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'intindexService' ];
	function createController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, intindexService) {
		var ctrl = this, intindexInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.intIndex = {};
		
		ctrl.lookUpAdd = function(row) {
			modalService.openCustomDialogs({
				'dialogsModule' : 'cda.indexLookUp',
				'btnCallback' : {
					'ok' : function(selectedRows){
						if(selectedRows){
							angular.merge(row.entity, selectedRows[0]);
							row.entity.dataStatus = "";
						}
					}
				}
			});
		};
		ctrl.init = function() {
				var params = {
						intIndexCode : intindexInfo.intIndexCode
				};
				//add validator
				if(ctrl.validatorConfig) ctrl.layoutConfig.formConfigs.general.tableConfigs.table.validatorConfig = ctrl.validatorConfig.intIndexList;
				ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.general.tableConfigs.table, rowSelectionChanged);
				ctrl.innerGridOptions.data = 'vm.intIndex.intIndexList';
				intindexService.initOptions(ctrl);
				authService.create(params).then(function(response) {
					ctrl.intIndex = response.data || intindexInfo;
					ctrl.intIndex.intIndexList = ctrl.intIndex.intIndexList || [];
					
				});
		};
		
		ctrl.addIntIndex = function(row) {
			row.tenorFlag = 'N';
		};
		
		ctrl.createSave = function() {
			if(ctrl.intIndex.intIndexList.length == 0){
				modalService.openErrorDialogs({
					'messages' : ['title.dialogs.intindex.add.record']
				});
				return;
			}
			if (ctrl.intIndex.taskId) {
				return authService.createContinueSave(ctrl.intIndex).then(saveSuccessful, saveFailed);
			} else {
				return authService.createSave(ctrl.intIndex).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createSubmit = function() {
			if(ctrl.intIndex.intIndexList.length == 0){
				modalService.openErrorDialogs({
					'messages' : ['title.dialogs.intindex.add.record']
				});
				return;
			}
			if (ctrl.intIndex.taskId) {
				return authService.createContinueSubmit(ctrl.intIndex).then(saveSuccessful, saveFailed);
			} else {
				return authService.createSubmit(ctrl.intIndex).then(submitSuccessful, submitFailed);
			}
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedIntIndex = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.intindex.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.intindex.list',
					'state' : 'cda.intindex.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.intIndex = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
